var app=angular.module('myApp', []).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider.
      when('/', {templateUrl: 'tran_search_ajs.html', controller: ListCustCtrl}).
      when('/cancel/', {templateUrl: 'tran_search_ajs.html', controller: ListCustCtrl}). 
      when('/edittran/', {templateUrl: 'tran_search_ajs.html', controller: ListCustCtrl}). 
      otherwise({redirectTo: '/'});
}]);


function ListCustCtrl($scope, $http,$window,$location, $filter)
 {
    $scope.currentPage = 0;
    $scope.pageSize = 15;
	$scope.Math = window.Math;
	$scope.ed = false;
	$scope.cityarr=[];
	$scope.modearr=[];
	    $scope.names = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"];


    $http.get('tran_search_data.php?id=1').success(function(data) {
		$scope.canceltblmast = data;
	});
	$http.get('tran_search_data.php?id=2').success(function(data) {
		for(var i=0;i<data.length;i++)
			$scope.modearr[i] = data[i];
	});
	$http.get('tran_search_data.php?id=3').success(function(data) {
		for(var i=0;i<data.length;i++)
			$scope.cityarr[i] = data[i];
	});
 
    
    $scope.edit = function(id) {
		for(i in $scope.canceltblmast) {         		
            if($scope.canceltblmast[i].trnid == id.trnid)
            {              					
               $scope.newtransactionmast = angular.copy($scope.canceltblmast[i]);   		
            }
        }  
		$scope.ed=true;		
    };
    
      
   $scope.deletetran = function(item) {
        if (confirm('Do you really want to delete this record?')) 
        {           
			var sql = 'tran_search_deletedata.php?trnid='+item;
			$window.alert(sql);
            $http.post(sql).success(function(data)
            {
                $window.alert( 'Record number ' +item+ ' got deleted Successfully');
                $scope.activePath = $location.path('tran_search_ajs.php#/edittran/');
                window.location.href = '#/edittran/';
            });              
        }
      };
      
	$scope.nullvalid=function(mast)
	{
		var ret='';
		if(!mast.policyno || !mast.inwarddate || !mast.courierid || !mast.districtid )			
			ret = 'Please fill all the fields marked with *';
		else 
			ret = 'y';
		return ret;
	}
	$scope.existvalid=function(mast)
	{
		var ret='';
		var ans = false;												
		for(var i = 0; i < $scope.canceltblmast.length; i++)
		{										
			if(!ans && mast.policyno == $scope.canceltblmast[i].policyno)			
				ans=true;																	
		}																				
		if(ans)
			ret += 'This policy number already exists...';											
		if(ret.length==0)
			ret = "y";					
		return ret;
	}
	
	$scope.cdvalid=function(mast)
	{
		var ret='';
		var ans = false;												
		var ans2 = false;												
		for(var i = 0; i < $scope.modearr.length; i++)
		{										
			if(!ans && mast.courierid.toLowerCase() == $scope.modearr[i].toLowerCase())			
				ans=true;																	
		}	
		for(var i = 0; i < $scope.cityarr.length; i++)
		{										
			if(!ans2 && mast.districtid.toLowerCase() == $scope.cityarr[i].toLowerCase())			
				ans2=true;																	
		}	
		if(!ans)
			ret += 'This courier mode is incorrect, does not exist...';
		if(!ans2)
			ret += '\nThis city name is incorrect, does not exist...';											
		if(ret.length==0)
			ret = "y";					
		return ret;
	}
	
	$scope.sv = function(dataset) 
	{       	
		var res1 = $scope.nullvalid(dataset);
		var res2 = $scope.existvalid(dataset);
		var res3 = $scope.cdvalid(dataset);
		if(res1 == "y")
		{
			if(res2 == "y")
			{
				if(res3 == "y")
				{
					var sql = 'tran_search_insertdata.php?dataset='+JSON.stringify(dataset)+'&action=i';		$http.post(sql).success(function(data)  
					{			
						$window.alert( 'Record inserted');		
						$scope.cancel();
					});
				}else alert(res3);
			}else alert(res2);
		}else alert(res1);					
	};
	
    $scope.updtcustdata = function(dataset) 
	{    
		var res1 = $scope.nullvalid(dataset);
		var res3 = $scope.cdvalid(dataset);		
		if(res1 == "y")
		{		
			if(res3 == "y")
			{				
				var sql = 'tran_search_insertdata.php?dataset='+JSON.stringify(dataset)+'&action=u';
				$http.post(sql).success(function(data)  
				{		
					$window.alert( 'Record number '+ dataset.trnid +' got Successfully Updated');
					$scope.cancel();
				});
			}else alert(res3);
		}else alert(res1);
	};
      
    $scope.cancel= function()
    {
        $scope.activePath = null;     
		$scope.activePath = $location.path('tran_search_ajs.php/edittran/');
		window.location.href = '#/edittran/';
    };
      
 }
 
	app.directive('autoComplete', function($timeout) {
    return function(scope, iElement, iAttrs) {
            iElement.autocomplete({
                source: scope[iAttrs.uiItems],
                select: function() {
                    $timeout(function() {
                      iElement.trigger('input');
                    }, 0);					
                }
            });
		};
	});   
           
     app.directive('capitalize', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, modelCtrl) {
                var capitalize = function (inputValue) {
                    if (angular.isUndefined(inputValue))
                        return;
 
                    var capitalized = inputValue.toUpperCase();
                    if (capitalized !== inputValue) {
                        modelCtrl.$setViewValue(capitalized);
                        modelCtrl.$render();
                    }
                    return capitalized;
                };
                modelCtrl.$parsers.push(capitalize);
                capitalize(scope[attrs.ngModel]);  // capitalize initial value
            }
        };
    });
    
        
    app.filter('startingFrom', function() 
    {
            return function(input, start) 
            {
                start = +start; //parse to int
                return input.slice(start);
            };
    });                 
